package com.neusoft.neuiotms.dcam.fixed.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.model.AssetLifeCycle;
import com.neusoft.neuiotms.dcam.fixed.model.QAssetLifeCycle;
import com.neusoft.neuiotms.dcam.fixed.repository.CustomizedAssetLifeCycleRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedAssetLifeCycleRepositoryImpl extends AbstractRepository
        implements CustomizedAssetLifeCycleRepository {

    @Override
    public List<AssetLifeCycle> search(final Searchable searchable) {
        final QAssetLifeCycle qAssetLifeCycle = QAssetLifeCycle.assetLifeCycle;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<AssetLifeCycle> query = this.query().selectFrom(qAssetLifeCycle);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<AssetLifeCycle> search(final Pageable pageable, final Searchable searchable) {
        final QAssetLifeCycle qAssetLifeCycle = QAssetLifeCycle.assetLifeCycle;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<AssetLifeCycle> query = this.query().selectFrom(qAssetLifeCycle);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return AssetLifeCycle.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QAssetLifeCycle qAssetLifeCycle = QAssetLifeCycle.assetLifeCycle;
        where.and(super.containsStr(qAssetLifeCycle.code, searchable, "code"));
        return where;
    }

}
